/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * QuestionList.java
 *
 * Created on Aug 29, 2011, 10:17:58 AM
 */
package cw.ui;

import cw.biz.PuzzleBiz;
import cw.biz.QuestionBiz;
import cw.entity.Question;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;

/**
 *
 * @author VinhTC
 */
public class QuestionList extends javax.swing.JDialog{

    public static int subjId = 1;
    AdminMainPanel amp = new AdminMainPanel();
    PuzzleBiz biz = new PuzzleBiz();
    QuestionBiz quesBiz = new QuestionBiz();
    public static String quest = "";
    private boolean okPressed;

    /** Creates new form QuestionList */
    public QuestionList(JFrame parent) {
        super(parent, "Connect", true);
        initComponents();
        InitialGui();

    }

    public static String getQuest() {
        return quest;
    }

    public static void setQuest(String quest) {
        QuestionList.quest = quest;
    }

    /**
     * InitialGui
     */
    public void InitialGui() {
        subjId = amp.getSubjectId();
        List<Question> quests = new ArrayList<Question>();

        quests = quesBiz.getQuestionBySubjectId(subjId);
        String[] listQues = new String[quests.size()];
        for (int i = 0; i < quests.size(); i++) {
            listQues[i] = quests.get(i).getQuestion();
        }

        jlstQuestions.setListData(listQues);
    }

    public boolean showDialog(String questionName) {
        okPressed = false;
        show();
        if (okPressed) {
            questionName = jlstQuestions.getSelectedValue().toString();
        }
        return okPressed;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jlstQuestions = new javax.swing.JList();
        jbtnImportQuestion = new javax.swing.JButton();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Questions list of subject"));
        jPanel1.setName("jPanel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jlstQuestions.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jlstQuestions.setName("jlstQuestions"); // NOI18N
        jlstQuestions.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jlstQuestionsValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jlstQuestions);

        jbtnImportQuestion.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/down.png"))); // NOI18N
        jbtnImportQuestion.setText("Import this question");
        jbtnImportQuestion.setName("jbtnImportQuestion"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 373, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jbtnImportQuestion)
                        .addGap(116, 116, 116))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jbtnImportQuestion)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jlstQuestionsValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jlstQuestionsValueChanged

        Object source = evt.getSource();

        if (source == jbtnImportQuestion) {
            okPressed = true;
            this.setVisible(false);
        }


    }//GEN-LAST:event_jlstQuestionsValueChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtnImportQuestion;
    private javax.swing.JList jlstQuestions;
    // End of variables declaration//GEN-END:variables
}
